package com.zjitc.service.imp;

import com.zjitc.common.ServerResponse;
import com.zjitc.dao.UserDao;
import com.zjitc.model.User;
import com.zjitc.service.UserService;
import javax.print.attribute.standard.JobOriginatingUserName;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;

/**
 * Create by IntelliJ IDEA
 *
 * @author: jsonor
 * @date-Time: 2018/3/28 14:34
 * @description:
 */

@Service
public class UserServiceImp implements UserService{

  @Autowired
  private Jedis jedis;

  @Autowired
  private UserDao userDao;

  @Override
  public ServerResponse<User> login(String username, String password) {
    int count = userDao.userNameCount(username);
    if (count != 1) {
      return ServerResponse.createError("user do not exist");
    }

    // Todo. password 需要加密
    User user = userDao.findByUserNameAndPassword(username, password);
    if (user == null) {
      return ServerResponse.createError("password error");
    }

    user.setPassword(null);
    user.setQuestion(null);
    user.setAnswer(null);
    return ServerResponse.createSuccess(user);
  }

  /**
   * type username, email
   * @param str
   * @param type
   */
  @Override
  public void vaild(String str, String type) {

    // 10w

    //if (jedis.exists(type)) {
    //  jedis.sismember(type, str);
    //} else {
    //  // 从数据库读取，存入到缓存中
    //}


    /*
    * exists -->
    * sismember --> key
    * sadd
    *
    * */
  }

  public void register() {
    // 将数据插入到数据库中
    //jedis.sadd("username", username);
    //jedis.sadd("email", email);
  }
}
